﻿using BaseAuthRole.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using RS.BaseModel.Common;
using RS.Tool.Init;

namespace BaseAuthRole.Web.Filters
{
    public class AppAccessCheckFilter : ActionFilterAttribute
    {
        //action执行后
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            
        }
        //action执行前
        public override void OnActionExecuting(ActionExecutingContext context)
        {

            var arguments = context.ActionArguments;
         

            if (arguments.Count != 0)
            {
                var model = (BaseAppInputModel)arguments.First().Value;

                var userService = ServiceLocator.Instance.GetService<IUserService>();

                var r = userService.AccessTokenCheck(model.AppId, model.AppAccessToken);

                if (r.IsError)
                {
                    //中文会被编码
                    context.Result = new JsonResult(r);
                    
                }

            }
            else
            {


                context.Result = new JsonResult(new CommonResult
                {

                    IsError = true,
                    Value = RS.BaseModel.Enum.EnumBaseResult.KeyParaMiss,
                    Message = "AppId和AppAccessToken不可为空"
                }); ;
                

            }



        }
    }
}
